<?php

namespace lxs\api;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\JsonResponse;

class ApiResponse
{
    /**
     * 响应接口数据
     */
    public static function apiResponseData(int $code, string $message, array $data = []): JsonResponse
    {
        $responseData = [
            'code'    => $code,
            'message' => $message,
        ];
        $responseData['data'] = $data;
        return response()->json($responseData);
    }

    public static function success($message, array|Arrayable $data = []): JsonResponse
    {
        $successCode = 200;
        if (is_array($message) || is_object($message)) {
            $data = $message;
            $message = '';
        }
        return self::apiResponseData($successCode, $message, $data);
    }

    public static function error(string $message, int $errorCode = 201, array $data = []): JsonResponse
    {
        return self::apiResponseData($errorCode, $message, $data);
    }
}
